from math import *
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import font_manager as fm
from matplotlib import rcParams
from matplotlib.ticker import MultipleLocator


def XY_Axis(x_start, x_end, y_start, y_end):
    plt.rcParams['figure.dpi'] = 1200

    fig = plt.figure(figsize=(5, 4))
    ax = fig.add_subplot(111)

    xmajor = MultipleLocator(0.5 * x_end)
    ymajor_1 = MultipleLocator(y_end * 0.25)

    ax.xaxis.set_major_locator(xmajor)
    ax.tick_params(axis="x", direction="out", which="major", labelsize=9, length=3)
    plt.xticks([x_start, (x_start + x_end)/2, x_end], ['0', 'arcsin($r/L$)', '2arcsin($r/L$)'])

    ax.set_yticks([])

    plt.text(x_end * 0.488, y_start - 0.028, "θ", fontproperties = fm.FontProperties(fname = 'C:/Windows/Fonts/TIMESI.TTF', size = 14, style = 'italic'))
    plt.text(x_start - 0.09, y_end * 0.465, "f$_{1}$(θ)", fontproperties = fm.FontProperties(fname = 'C:/Windows/Fonts/TIMESI.TTF', size = 14, style = 'italic'), rotation = 90)


def D_PDF_OneP(L, r, mAngle, N):
    L2 = L * L
    r2 = r * r
    x = np.linspace(0.0, 2 * mAngle, N * 2, endpoint = False)
    y = x - x
    HalfXL = x[1] * 0.5
    for i in range(0, N * 2):
        x[i] += HalfXL
    Ymax = 0.0
    j = 0
    for xi in x:
        c = cos(xi - mAngle)
        s = sin(xi - mAngle)
        s2 = s * s
        ti = r2 - L2 * s2
        st = sqrt(ti)
        tj = 0.1 * 2 * L * c * st / (pi * r2)
        if tj > Ymax:
            Ymax = tj
        y[j] = tj
        j += 1
    XY_Axis(0.0, 2 * mAngle, 0.0, Ymax)
    plt.plot(x, y, "k-", linewidth=1, linestyle="-")
    plt.savefig("Fig2.jpg")


N = 1000
L = 100
r = 35
mAngle = asin(r / L)
D_PDF_OneP(L, r, mAngle, N)
plt.show()
